package com.gglx.flex.components
{
	import mx.core.IDataRenderer;

	import spark.components.DataGroup;
	import spark.components.IItemRenderer;

	public class GGItemRenderer extends GGUIComponent implements IDataRenderer, IItemRenderer
	{
		private var _data:Object;

		private var _dragging:Boolean = false;
		private var _label:String = "";
		private var _itemIndex:int;
		private var isLastItem:Boolean = false;
		private var _selected:Boolean = false;
		private var _showsCaret:Boolean = false;

		public function GGItemRenderer()
		{
			super();
		}

		override protected function createChildren():void
		{
			super.createChildren();
		}

		override protected function measure():void
		{
			super.measure();
		}

		override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.drawBackground(unscaledWidth, unscaledHeight);
		}

		override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.layoutContents(unscaledWidth, unscaledHeight);
		}

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}

		public function get itemIndex():int
		{
			return _itemIndex;
		}

		public function set itemIndex(value:int):void
		{
			var wasLastItem:Boolean = isLastItem;
			var dataGroup:DataGroup = parent as DataGroup;
			isLastItem = (dataGroup && (value == dataGroup.numElements - 1));

			// if whether or not we are the last item in the last has changed then
			// invalidate our display. note:  even if our new index has not changed,
			// whether or not we're the last item may have so we perform this check 
			// before the value == _itemIndex check below
			if (wasLastItem != isLastItem)
			{
				invalidateDisplayList();
			}

			if (value == _itemIndex)
				return;

			_itemIndex = value;
		}

		public function get dragging():Boolean
		{
			return _dragging;
		}

		public function set dragging(value:Boolean):void
		{
			_dragging = value;
		}

		public function get label():String
		{
			return _label;
		}

		public function set label(value:String):void
		{
			if (value == _label)
			{
				return;
			}

			_label = value;
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			if (value == _selected)
			{
				return;
			}

			_selected = value;
			invalidateDisplayList();
		}

		public function get showsCaret():Boolean
		{
			return _showsCaret;
		}

		public function set showsCaret(value:Boolean):void
		{
			if (value == _showsCaret)
			{
				return;
			}

			_showsCaret = value;
			invalidateDisplayList();
		}
	}
}
